package com.rookieci.totp.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.rookieci.lib.TimeConstant;
import com.rookieci.totp.TotpApp;
import com.rookieci.totp.mode.QRCodeMode;
import com.rookieci.utils.MD5Util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 磁磁帅 on 2025/8/10 0010.
 * 功能：
 */
public class QRCodeDB {
    public QRCodeDB() {
    }

    /**
     * @param type 1条码，2二维码
     */
    public void insert(String codeStr, String type) {
        //codestr,type,timestr
        long millis = TimeConstant.currentTimeMillis();
        DBHelp dbHelp = new DBHelp(TotpApp.getInstance());
        SQLiteDatabase database = dbHelp.getWritableDatabase();
        //先去重
        String md5 = MD5Util.getMD5(codeStr);
        Cursor cursor = database.rawQuery("select _id from qrcodeTb where md5 = ? and type = ?;", new String[]{md5, type});
        int Id = -1;
        if (cursor.moveToNext()) {
            int idx = cursor.getColumnIndex("_id");
            if (idx >= 0) Id = cursor.getInt(idx);
        }
        cursor.close();
        if (Id > 0) {//存在-更新下时间，用于排序
            database.execSQL("update qrcodeTb set timestr = ? where _id = ?;", new Object[]{millis, Id});
        } else {
            database.execSQL("insert into qrcodeTb(codestr,type,timestr,md5) values(?,?,?,?);", new Object[]{codeStr, type, millis, md5});
        }
        database.close();
        dbHelp.close();
    }

    public void delect(int _id) {
        if (_id == 0) return;
        DBHelp dbHelp = new DBHelp(TotpApp.getInstance());
        SQLiteDatabase database = dbHelp.getWritableDatabase();
        database.execSQL("delete from qrcodeTb where _id = ?;", new Object[]{_id});
        database.close();
        dbHelp.close();
    }

    /**
     * 查找所有二维码
     */
    public List<QRCodeMode> selectAll() {
        DBHelp dbHelp = new DBHelp(TotpApp.getInstance());
        SQLiteDatabase database = dbHelp.getReadableDatabase();
        //create table qrcodeTb(_id integer primary key autoincrement,codestr,type,timestr,md5)
        //从大到小
        Cursor cursor = database.rawQuery("select * from qrcodeTb ORDER BY timestr DESC;", null);
        List<QRCodeMode> modeList = getModeListFromCursor(cursor);
        cursor.close();
        database.close();
        dbHelp.close();

        return modeList;
    }

    private List<QRCodeMode> getModeListFromCursor(Cursor cursor) {
        List<QRCodeMode> modeList = new ArrayList<>();
        while (cursor.moveToNext()) {
            QRCodeMode mode = new QRCodeMode();
            int idx = cursor.getColumnIndex("_id");
            if (idx >= 0) mode._id = cursor.getInt(idx);
            idx = cursor.getColumnIndex("codestr");
            if (idx >= 0) mode.codeStr = cursor.getString(idx);
            idx = cursor.getColumnIndex("type");
            if (idx >= 0) mode.type = cursor.getString(idx);
            idx = cursor.getColumnIndex("timestr");
            if (idx >= 0) mode.timeL = cursor.getLong(idx);
            idx = cursor.getColumnIndex("md5");
            if (idx >= 0) mode.md5 = cursor.getString(idx);
            modeList.add(mode);
        }
        return modeList;
    }
}
